
package session.stateless.MRP.ProductionPlan;


import entity.MRP.ProductionPlan.PlannedDailyDemand;
import entity.MRP.ProductionPlan.PlannedMonthlyDemand;
import entity.MRP.ProductionPlan.PlannedWeeklyDemand;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;


@Stateless
public class DailyProductionPlanBean implements DailyProductionPlanBeanLocal {

    @PersistenceContext 
    private EntityManager em;
    @EJB
    private WeeklyProductionPlanBeanLocal weeklyProductionPlanBeanLocal;
    
    public List<PlannedDailyDemand> createPlannedDailyDemands(String week)
    {
        List<PlannedDailyDemand> dayList = new ArrayList<PlannedDailyDemand>();
        int startDay = Integer.parseInt(week.substring(0, week.indexOf("/")));
        String temp = week.substring(week.indexOf("/") + 1);
        int endDay = Integer.parseInt(temp.substring(temp.indexOf("-")+1, temp.indexOf("/")));
        int month = Integer.parseInt(temp.substring(temp.indexOf("/") + 1));

        //sett WeeklyDemand
        Query q = em.createQuery("SELECT p FROM PlannedWeeklyDemand p WHERE p.weekPeriod=:week");
        q.setParameter("week", week);
        PlannedWeeklyDemand plannedWeeklyDemand = (PlannedWeeklyDemand) q.getSingleResult();
        PlannedMonthlyDemand plannedMonthlyDemand = plannedWeeklyDemand.getPlannedMonthlyDemand();
        
        Long monthlyDemandA = new Long(0);
        Long monthlyDemandB = new Long(0);
        Long monthlyDemandC = new Long(0);
        Long monthlyDemandD = new Long(0);
        
        
        if (plannedMonthlyDemand.getProductionPlanA() != null)
        {
            monthlyDemandA = plannedMonthlyDemand.getProductionPlanA();
        }
        if (plannedMonthlyDemand.getProductionPlanB() != null)
        {
            monthlyDemandB = plannedMonthlyDemand.getProductionPlanB();
        }
        if (plannedMonthlyDemand.getProductionPlanC() != null)
        {
            monthlyDemandC = plannedMonthlyDemand.getProductionPlanC();
        }
        if (plannedMonthlyDemand.getProductionPlanD() != null)
        {
            monthlyDemandD = plannedMonthlyDemand.getProductionPlanD();
        }
        
        Long workingDays = plannedMonthlyDemand.getNumberOfWorkingDays();
        
        for (int i = startDay; i <= endDay; ++i)
        {
            PlannedDailyDemand p = new PlannedDailyDemand();
            p.setPlannedWeeklyDemand(plannedWeeklyDemand);
            p.setDayName(Integer.toString(i) + "/" + Integer.toString(month));
            
            p.setDailyDemandA(Math.round((double) monthlyDemandA / (double) workingDays));
            p.setDailyDemandB(Math.round((double) monthlyDemandB / (double) workingDays));
            p.setDailyDemandC(Math.round((double) monthlyDemandC / (double) workingDays));
            p.setDailyDemandD(Math.round((double) monthlyDemandD / (double) workingDays));
            
            em.persist(p);
            
            em.flush();
            
            //plannedWeeklyDemand.getPlannedDailyDemand().add(p);
            
            //em.flush();
            dayList.add(p);
        }        
        
        return dayList;
    }
    
    public void updatePlannedDailyDemands(Long id)
    {
        PlannedDailyDemand p = em.find(PlannedDailyDemand.class, id);

        String date = p.getDayName();

        int day = Integer.parseInt(date.substring(0, date.indexOf("/")));
        int month = Integer.parseInt(date.substring(date.indexOf("/") + 1));

        PlannedWeeklyDemand plannedWeeklyDemand = p.getPlannedWeeklyDemand();
        PlannedMonthlyDemand plannedMonthlyDemand = plannedWeeklyDemand.getPlannedMonthlyDemand();
        
        Long monthlyDemandA = plannedMonthlyDemand.getProductionPlanA();
        Long monthlyDemandB = plannedMonthlyDemand.getProductionPlanB();
        Long monthlyDemandC = plannedMonthlyDemand.getProductionPlanC();
        Long monthlyDemandD = plannedMonthlyDemand.getProductionPlanD();

        Long workingDays = plannedMonthlyDemand.getNumberOfWorkingDays();
  
        p.setDailyDemandA(Math.round((double) monthlyDemandA / (double) workingDays));
        p.setDailyDemandB(Math.round((double) monthlyDemandB / (double) workingDays));
        p.setDailyDemandC(Math.round((double) monthlyDemandC / (double) workingDays));
        p.setDailyDemandD(Math.round((double) monthlyDemandD / (double) workingDays));

        em.persist(p);

        em.flush();
    }
}
